﻿using System;
using System.Collections;

namespace Inovout.Data.Repository.Linq
{
    internal class FilterCachedKey
    {
        private readonly Type ModelType;
        public FilterCachedKey(Type modelType, FilterItemExpression[] items)
        {
            ModelType = modelType;
            this.Items = items??new FilterItemExpression[0];
            HashCodeCombiner hashCodeCombiner = new HashCodeCombiner();
            hashCodeCombiner.Add(ModelType);
            hashCodeCombiner.Add(Items);
            hashCode = hashCodeCombiner.CombinedHash;

        }
        public FilterItemExpression[] Items { get; private set; }
        public override bool Equals(object obj)
        {
            FilterCachedKey that = obj as FilterCachedKey;
            if (that == null)
            {
                return false;
            }
            return this.Items.Length == that.Items.Length && 0 == (this.Items as IStructuralComparable).CompareTo(that.Items, StructuralComparisons.StructuralComparer);
        }
        private int hashCode;
        public override int GetHashCode()
        {
            return hashCode;
        }
    }
}
